/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.model.core;

import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.xml.XMLUtility;


/**
 * @author zl
 *
 */
public class MetaRelationship extends MetaClass {
	protected MetaClass sourceType;
	protected MetaClass targetType;
	/**
	 * @return the sourceType
	 */
	@MethodProperty(
			id = "RELATIONSHIP_SOURCETYPE",
			catalog = "Model",
			name = CorePackage.RELATIONSHIP_SOURCETYPE,
			setter = "setSourceType"
			)
	@EnumerationProperty(
			function = "getEndTypes"
	)
	public MetaClass getSourceType() {
		return sourceType;
	}
	
	public Object[] getEndTypes() {
		List types = model.getInstancesForType(CorePackage.__METACLASS__);
		return types.toArray();
	}
	/**
	 * @param sourceType the sourceType to set
	 */
	public void setSourceType(MetaClass sourceType) {
		this.sourceType = sourceType;
	}
	/**
	 * @return the targetType
	 */
	@MethodProperty(
			id = "RELATIONSHIP_TARGETTYPE",
			catalog = "Model",
			name = CorePackage.RELATIONSHIP_TARGETTYPE,
			setter = "setTargetType"
			)
	@EnumerationProperty(
			function = "getEndTypes"
	)
	public MetaClass getTargetType() {
		return targetType;
	}
	/**
	 * @param targetType the targetType to set
	 */
	public void setTargetType(MetaClass targetType) {
		this.targetType = targetType;
	}
	
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodeNotContain(root, CorePackage.RELATIONSHIP_SOURCETYPE, sourceType);
		XMLUtility.xmlInstance.serializeNodeNotContain(root, CorePackage.RELATIONSHIP_TARGETTYPE, targetType);
	}
	
	public void deSerialize(Element root) {
		super.deSerialize(root);
		sourceType = (MetaClass)XMLUtility.xmlInstance.deSerializeNodeNotContain(root, CorePackage.RELATIONSHIP_SOURCETYPE, this);
		targetType = (MetaClass)XMLUtility.xmlInstance.deSerializeNodeNotContain(root, CorePackage.RELATIONSHIP_TARGETTYPE, this);
	}
	
	public void dynamicSet(String featureID, Object value) {
		if(featureID.equals(CorePackage.RELATIONSHIP_SOURCETYPE)) {
			sourceType = (MetaClass)value;
		}
		else if(featureID.equals(CorePackage.RELATIONSHIP_TARGETTYPE)) {
			targetType = (MetaClass)value;
		}
		else {
			super.dynamicSet(featureID, value);
		}
	}
	
	@Override
	protected EClass eStaticClass() {
		return EMetaModelPackage.Literals.EMETA_RELATIONSHIP;
	}
	
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EMetaModelPackage.EMETA_RELATIONSHIP__SOURCE_TYPE:
				if (resolve) return getSourceType();
				return getSourceType();
			case EMetaModelPackage.EMETA_RELATIONSHIP__TARGET_TYPE:
				if (resolve) return getTargetType();
				return getTargetType();
		}
		return super.eGet(featureID, resolve, coreType);
	}
}
